package com.wondersgroup.fjzlpt.org.util;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.quick.framework.boot.web.wapper.QuickPrincipal;
import com.quick.framework.util.ExcelOrWordUtils;
import com.quick.framework.util.QuickPrincipalUtils;
import com.wondersgroup.fjzlpt.org.OrgServiceApp;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * 访问层工具类
 * <p>
 * <p>
 * Created by sanma on 2017/8/24.
 */
public class ControllerCommonUtil extends  ProjectParams{



    /**
     * 通过缓存获取页面按钮
     * @param request
     * @return
     */
    public static String getMenuButton(HttpServletRequest request){
        return QuickPrincipalUtils.getMenuButton(request,projectName);
    }

    public static Map getQueryTdxxParams(HttpServletRequest request){
        Map map = RequestUtils.GetParameterMap(request);
        String jglb =  QuickPrincipalUtils.getJglbdm(request);
        String xzqhdm = QuickPrincipalUtils.getXzqhdm(request);
        String roleid = QuickPrincipalUtils.getRoleID(request);
        String userid = QuickPrincipalUtils.getUserID(request);
        String PTJGID = QuickPrincipalUtils.getPtjgID(request);
        map.put("XZQHDM",xzqhdm);
        map.put("TDZT" ,"1");
        //if(jglb.equals("C") || jglb.equals("B")){
        //    if(roleid.equals(ysRole) || roleid.equals(ystdzRole)){
        //        map.put("USERID",userid);
        //    }else{
        //        map.put("PTJGID",PTJGID);
        //    }
        //}else if(jglb.indexOf("R") ==  -1){
        //    map.put("PTJGID",PTJGID);
        //    if(roleid.equals(ysRole) || roleid.equals(ystdzRole)){
        //        map.put("USERID",userid);
        //    }
        //}
        if(jglb.indexOf("R") ==  -1){
            map.put("PTJGID",PTJGID);
            if(roleid.equals(ysRole) || roleid.equals(ystdzRole)){
                map.put("USERID",userid);
            }
        }
        return map;
    }

    /**
     * 导出word文件
     * @param dataMap 替换word模板数据
     * @param response 响应
     * @param modelName 模板文件名
     */
    public static void downLoadWordFile(Map<String,Object> dataMap, HttpServletResponse response, String modelName){
        ExcelOrWordUtils.downLoadWordFile(dataMap , response , modelName , realPath);
    }

    /**
     * EasyUI的分页参数转换
     * @param map
     */
    public static void pageFormat(Map map){
        Object rows = map.remove("rows");
        if(rows != null){
            map.put("pageSize", rows.toString());
        }
        Object page = map.remove("page");
        if(page != null){
            map.put("pageNumber", page.toString());
        }
    }

    public static JSONObject getRequestData(HttpServletRequest request){
        JSONObject requestData = new JSONObject(1);
		/*是否是管理者*/
        boolean isManager = true;
        QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
        String WSJGLBDM = principal.getUserInfo().getString("WSJGLBDM");
        if(!StringUtils.isEmpty(WSJGLBDM) && WSJGLBDM.indexOf("R") != 0){
            isManager = false;
            JSONObject userInfo = principal.getUserInfo();
            requestData = userInfo;
        }
        requestData.put("isManager", isManager);
        return requestData;
    }

}
